<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配送员ID" prop="riderId">
        <el-input
          v-model="queryParams.riderId"
          placeholder="请输入配送员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="求助时间" prop="helpTime">
        <el-date-picker clearable
          v-model="queryParams.helpTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择求助时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="requestList" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="配送员ID" align="center" prop="riderId" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="求助类型" align="center" prop="helpType" />
      <el-table-column label="求助描述" align="center" prop="helpDesc" />
      <el-table-column label="求助位置" align="center" prop="helpLocation" />
      <el-table-column label="求助时间" align="center" prop="helpTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.helpTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改求助管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="requestRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Request">
import { listRequest, getRequest, delRequest, addRequest, updateRequest } from "@/api/system/riderRequest.js"

import {useRoute, useRouter} from 'vue-router';

const { proxy } = getCurrentInstance()

const requestList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    riderId: null,
    realName: null,
    phoneNumber: null,
    helpType: null,
    helpTime: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)
const router = useRouter();
function handleRowClick(row) {
  router.push({
    name: 'requestDetail',
    params: {id: row.id,
      status: row.status,

    }

  });
}
/** 查询求助管理列表 */
function getList() {
  loading.value = true
  listRequest(queryParams.value).then(response => {
    requestList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    riderId: null,
    realName: null,
    phoneNumber: null,
    helpType: null,
    helpDesc: null,
    helpLocation: null,
    helpTime: null,
    status: null
  }
  proxy.resetForm("requestRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加求助管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRequest(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改求助管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["requestRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRequest(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRequest(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除求助管理编号为"' + _ids + '"的数据项？').then(function() {
    return delRequest(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/request/export', {
    ...queryParams.value
  }, `request_${new Date().getTime()}.xlsx`)
}

getList()
</script>
